package com.blog.cmrpersonalblog.dto.article.response;

import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户端文章详情响应DTO
 * 专门为前端用户阅读文章设计，去除管理信息，增加用户交互数据
 */
@Data
public class UserArticleDetailResponse {

    /**
     * 文章ID
     */
    private Long id;

    /**
     * 文章标题
     */
    private String title;

    /**
     * 文章摘要
     */
    private String summary;

    /**
     * 文章内容（Markdown格式）
     */
    private String content;

    /**
     * 文章内容（HTML格式）
     */
    private String htmlContent;

    /**
     * 编辑器类型 1-Markdown编辑器 2-富文本编辑器
     */
    private Integer editorTypes;

    /**
     * 封面图片URL
     */
    private String coverImage;

    /**
     * 作者信息
     */
    private AuthorInfo author;

    /**
     * 分类信息
     */
    private CategoryInfo category;

    /**
     * 标签列表
     */
    private List<TagInfo> tags;

    /**
     * 浏览量
     */
    private Integer viewCount;

    /**
     * 点赞数
     */
    private Integer likeCount;

    /**
     * 评论数
     */
    private Integer commentCount;

    /**
     * 收藏数
     */
    private Integer collectCount;

    /**
     * 分享数
     */
    private Integer shareCount;

    /**
     * 是否原创 0-转载 1-原创
     */
    private Integer isOriginal;

    /**
     * 是否置顶 0-否 1-是
     */
    private Integer isTop;

    /**
     * 文章状态 0-草稿 1-已发布 2-已删除 3-待审核 4-审核拒绝 5-已下架
     */
    private Integer status;

    /**
     * 发布时间
     */
    private LocalDateTime publishTime;

    /**
     * 字数统计
     */
    private Integer wordCount;

    /**
     * 预计阅读时长（分钟）
     */
    private Integer readingTime;

    /**
     * 当前用户是否已点赞
     */
    private Boolean isLiked;

    /**
     * 当前用户是否已收藏
     */
    private Boolean isCollected;

    /**
     * 相关文章推荐
     */
    private List<RelatedArticleInfo> relatedArticles;

    /**
     * SEO相关信息
     */
    private SeoInfo seo;

    /**
     * 作者信息
     */
    @Data
    public static class AuthorInfo {
        private Long userId;
        private String userName;
        private String nickName;
        private String avatar;
        private String bio;
        private Integer articleCount;
        private Integer fansCount;
        private Boolean isFollowed; // 当前用户是否已关注此作者
    }

    /**
     * 分类信息
     */
    @Data
    public static class CategoryInfo {
        private Long id;
        private String name;
        private String description;
        private String icon;
    }

    /**
     * 标签信息
     */
    @Data
    public static class TagInfo {
        private Long id;
        private String name;
        private String color;
        private String description;
        private String icon;
        private Integer useCount;
    }

    /**
     * 相关文章信息
     */
    @Data
    public static class RelatedArticleInfo {
        private Long id;
        private String title;
        private String summary;
        private String coverImage;
        private Integer viewCount;
        private Integer likeCount;
        private LocalDateTime publishTime;
    }

    /**
     * SEO信息
     */
    @Data
    public static class SeoInfo {
        private String keywords;
        private String description;
        private String ogTitle;
        private String ogDescription;
        private String ogImage;
    }

    /**
     * 获取原创标识文本
     */
    public String getOriginalText() {
        if (isOriginal == null) {
            return "未知";
        }
        return isOriginal == 1 ? "原创" : "转载";
    }

    /**
     * 检查是否为置顶文章
     */
    public boolean isTopArticle() {
        return isTop != null && isTop == 1;
    }

    /**
     * 获取格式化的阅读时长
     */
    public String getFormattedReadingTime() {
        if (readingTime == null || readingTime <= 0) {
            return "1分钟";
        }
        if (readingTime < 60) {
            return readingTime + "分钟";
        } else {
            int hours = readingTime / 60;
            int minutes = readingTime % 60;
            return hours + "小时" + (minutes > 0 ? minutes + "分钟" : "");
        }
    }

    /**
     * 获取格式化的字数统计
     */
    public String getFormattedWordCount() {
        if (wordCount == null || wordCount <= 0) {
            return "0字";
        }
        if (wordCount < 10000) {
            return wordCount + "字";
        } else {
            double wan = wordCount / 10000.0;
            return String.format("%.1f万字", wan);
        }
    }
}
